package com.example.software.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.software.dto.DiagnosisDTO;
import com.example.software.entity.RecodeEntity;
import com.example.software.entity.Result;
import com.example.software.entity.WorkdayEntity;
import com.example.software.service.*;
import com.example.software.vo.DiagnosisVO;
import com.example.software.vo.RecodeVO;
import com.example.software.vo.workdayVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.DiagnosisEntity;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;



/**
 * 诊断表

 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-28 10:54:37
 */
@RestController
@Slf4j
@Api(tags = "诊断相关接口")
@RequestMapping("software/diagnosis")
public class DiagnosisController {
    @Autowired
    private DiagnosisService diagnosisService;

    @Autowired
    private RecodeService recodeService;

    @Autowired
    private MedicineService medicineService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private WorkdayService workdayService;

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "诊断信息列表查询")
   // @RequiresPermissions("software:diagnosis:list")
    public Result<PageUtils> list(@RequestParam String page,
                                  @RequestParam String size){

        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",size);

        PageUtils page2 = diagnosisService.queryPage(params);

        return Result.success(page2);
    }


    /**
     * 信息
     */

    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id查询诊断信息")
    // @RequiresPermissions("software:diagnosis:info")
    public Result<DiagnosisEntity> info(@PathVariable("id") Integer id){

        //拼接上预约完整信息和药品完整信息,还有支付相关
        log.info("查询诊断信息");

		DiagnosisEntity diagnosis = diagnosisService.getById(id);


        return  Result.success(diagnosis);
    }

    @GetMapping("/info2/{recodeId}")
    @ApiOperation(value = "根据预约id查询诊断信息")
    // @RequiresPermissions("software:diagnosis:info")
    public Result<DiagnosisVO> info2(@PathVariable("recodeId") Integer recodeId){

        //拼接上预约完整信息和药品完整信息,还有支付相关
        log.info("查询诊断信息");

        DiagnosisEntity diagnosis = diagnosisService.getByRecodeId(recodeId);

        if (diagnosis == null) {
            return Result.error("未找到相关诊断信息");
        }

        DiagnosisVO diagnosisVO = new DiagnosisVO();
        BeanUtils.copyProperties(diagnosis, diagnosisVO);

        RecodeEntity recode = recodeService.getById(recodeId);

        RecodeVO recodeVO = new RecodeVO();

        recodeVO.setDoctor(doctorService.getById(recode.getDoctorId()));
        recodeVO.setUser(userService.getById(recode.getUserId()));


        WorkdayEntity workdayEntity = workdayService.getById(recode.getWorkdayId());
        workdayVO workdayVO = new workdayVO();
        BeanUtils.copyProperties(workdayEntity,workdayVO);
        workdayVO.setDepartment(departmentService.getById(workdayEntity.getDepartmentId()));
        recodeVO.setWorkday(workdayVO);
//        recodeVO.setWorkday(workdayVO);

        BeanUtils.copyProperties(recode, recodeVO);
        diagnosisVO.setRecode(recodeVO);


        diagnosisVO.setMedicine(medicineService.listByIds(Arrays.asList(diagnosis.getMedicineId().replace("[","").replace("]","").split(","))));

        return  Result.success(diagnosisVO);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增诊断信息")
    // @RequiresPermissions("software:diagnosis:save")
    public R save(@RequestBody DiagnosisDTO diagnosisDTO){

        DiagnosisEntity diagnosis = new DiagnosisEntity();

        BeanUtils.copyProperties(diagnosisDTO, diagnosis);

        String medicine = diagnosisDTO.getMedicine_id().toString();
        diagnosis.setMedicineId(medicine);

		diagnosisService.save(diagnosis);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改诊断信息")
    // @RequiresPermissions("software:diagnosis:update")
    public R update(@RequestBody DiagnosisDTO diagnosisDTO){

        DiagnosisEntity diagnosis = new DiagnosisEntity();

        BeanUtils.copyProperties(diagnosisDTO, diagnosis);

        String medicine = diagnosisDTO.getMedicine_id().toString();

        diagnosis.setMedicineId(medicine);

		diagnosisService.updateById(diagnosis);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除诊断信息")
    // @RequiresPermissions("software:diagnosis:delete")
    public R delete(@RequestBody Integer[] ids){
		diagnosisService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
